//
//  FKChooseCouponPopView.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/11.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKChooseCouponPopView.h"
#import "FKChooseCouponCell.h"

@interface FKChooseCouponPopView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UIView *container;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UIButton *confirmBtn;

@end

@implementation FKChooseCouponPopView

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    CGFloat containerH = fk_adjustW(300);
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, fkScreenH, fkScreenW, containerH)];
    [self addSubview:container];
    self.container = container;
    container.backgroundColor = fkBgColor;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:@"优惠详情"];
    [container addSubview:titleLabel];
    
    UIButton *closeBtn = [UIButton fk_btnWithTarget:self action:@selector(closeBtnClicked) img:[UIImage imageNamed:@"close_icon"]];
    [container addSubview:closeBtn];
    
    UITableView *tableView = [[UITableView alloc] init];
    [container addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor clearColor];
    
    UIView *bottomView = [[UIView alloc] init];
    [container addSubview:bottomView];
    bottomView.backgroundColor = [UIColor whiteColor];
    
    UIButton *confirmBtn = [UIButton fk_btnWithTarget:self action:@selector(confirmBtnClicked) font:fk_adjustFont(16) normalTitle:@"完成" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_main"] selectedBgImg:nil];
    [bottomView addSubview:confirmBtn];
    self.confirmBtn = confirmBtn;
    [confirmBtn fk_viewCornerRadius:fk_adjustW(20) borderWidth:0 borderColor:nil];
    confirmBtn.enabled = false;

    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.centerX.offset(0);
        make.height.offset(fk_adjustW(44));
    }];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(44));
        make.top.right.offset(0);
    }];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(50));
    }];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.height.offset(fk_adjustW(40));
        make.centerY.offset(0);
    }];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(titleLabel.mas_bottom);
        make.bottom.equalTo(bottomView.mas_top);
    }];
}

- (void)closeBtnClicked
{
    [self close];
}

- (void)confirmBtnClicked
{
    if (self.chooseCouponBlock) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDictionary *data = self.datas[indexPath.row];
        self.chooseCouponBlock(data);
    }
    [self close];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKChooseCouponCell *cell = [FKChooseCouponCell cellWith:tableView];
    cell.data = self.datas[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(50);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    self.frame = window.bounds;
    [self.tableView reloadData];
    if (self.datas.count > 0) {
        self.confirmBtn.enabled = true;
//        dispatch_async(dispatch_get_main_queue(), ^{
//            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//            [self.tableView selectRowAtIndexPath:indexPath animated:true scrollPosition:UITableViewScrollPositionNone];
//            [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
//        });
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        self.container.fk_top = fkScreenH - self.container.fk_height;
    } completion:^(BOOL finished) {
        self.backgroundColor = fkColorAlpha(0, 0, 0, 0.5);
    }];
}

- (void)close
{
    [UIView animateWithDuration:0.25 animations:^{
        self.container.fk_top = fkScreenH;
        self.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


@end
